package com.beyond.pm.component.chart

import android.view.ViewGroup
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.viewinterop.AndroidView
import com.beyond.pm.R
import com.beyond.theme.orange500
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet

/**
 * @author chen
 * @time 2023/6/12 13:49
 * @name ProjectManage
 * @class describe
 */
@Composable
fun LineChart(
    modifier: Modifier = Modifier,
    entries: List<Entry>,
    dataSetLabel: String,
    dataSetColor: Color = MaterialTheme.colorScheme.primary,
    animateX: Int = 500,
    animateY: Int = 500
) {
    var chart by remember { mutableStateOf<LineChart?>(null) }
    val tColor = MaterialTheme.colorScheme.onBackground.toArgb()
    Box(modifier = modifier.fillMaxSize()) {
        Surface(color = MaterialTheme.colorScheme.background) {
            AndroidView(
                factory = { context ->
                    LineChart(context).apply {
                        axisLeft.apply {
                            setDrawAxisLine(true)
                            enableGridDashedLine(10f, 10f,10f)
                            setDrawGridLines(true)
                            axisLineWidth = 1f
                            axisLineColor = tColor
                            gridColor = tColor
                            textColor = tColor
                            spaceTop = 25f
                            spaceBottom = 25f
                            axisMinimum = 0f
                        }
                        xAxis.apply {
                            setDrawAxisLine(true) // X坐标轴的线
                            setDrawGridLines(false) //
                            axisLineWidth = 1f
                            setAvoidFirstLastClipping(true)
                            position = XAxis.XAxisPosition.BOTTOM
                            textColor = tColor
                            axisLineColor = tColor
                        }
                        axisRight.apply {
                            this.isEnabled = false
                        }
                        layoutParams = ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT
                        )
                        data = LineData().apply {
                        }
                        description = Description().apply {
                            text = ""
                        }
                        legend.apply {
                            isEnabled = false
                            textColor = tColor
                            this.orientation = Legend.LegendOrientation.HORIZONTAL
                        }
                        isHighlightPerTapEnabled = false
                        isHighlightPerDragEnabled = false
                        setNoDataText(context.getString(R.string.no_data))
                        setTouchEnabled(true)
                        isDragEnabled = true
                        setScaleEnabled(false)
                        setPinchZoom(true)
                        setDrawGridBackground(false)
                        setDrawBorders(false) // 边框
                        animateX(animateX)
//                        animateX(animateX)
                    }
                },
                update = { view ->
                    chart = view
                    updateChart(chart, entries, dataSetLabel, dataSetColor)
                }
            )
        }
    }
}

private fun updateChart(
    chart: LineChart?,
    entries: List<Entry>,
    dataSetLabel: String,
    dataSetColor: Color
) {
    chart?.let {
        val dataSet = LineDataSet(entries, dataSetLabel)
//        dataSet.setDrawValues(true)
        dataSet.color = dataSetColor.toArgb()
        dataSet.mode  = LineDataSet.Mode.CUBIC_BEZIER
        dataSet.valueTextSize = 8f
        dataSet.valueTextColor = orange500.toArgb()
        val lineData = LineData(dataSet)
        if (entries.isNotEmpty()) {
            it.data = lineData
        } else {
            it.data = null
        }
        it.invalidate()
    }
}